﻿using System;
using System.Net;
using System.Windows.Forms;
using ChaterFuncLib;

namespace ChaterServer
{
    public partial class Settings : Form
    {
        Mainform baseform;

        /// <summary>
        /// Конструктор формы с настройками
        /// </summary>
        /// <param name="settingparam">Тип изменяемой настройки</param>
        public Settings(Mainform baseform)
        {
            InitializeComponent();

            serverip.Text = Config.GetConfig("Network", "Host");
            serverip.ReadOnly = serverip.Text == "127.0.0.1";
            serverport.Text = Config.GetConfig("Network", "Port");
            serverclosed.Checked = Config.GetConfig("Network", "Closed") == "1";

            sqlip.Text = Config.GetConfig("MySQL", "Host");
            sqlip.ReadOnly = sqlip.Text == "127.0.0.1";
            sqldb.Text = Config.GetConfig("MySQL", "Database");
            sqldb.ReadOnly = sqldb.Text == "chater";
            sqluser.Text = Config.GetConfig("MySQL", "Username");
            sqluser.ReadOnly = sqluser.Text == "root";
            sqlpass.Text = Config.GetConfig("MySQL", "Password");

            localhost.Checked = serverip.Text == "127.0.0.1";
            sqllocalhost.Checked = sqlip.Text == "127.0.0.1";
            sqlstandartdb.Checked = sqldb.Text == "chater";
            sqlroot.Checked = sqluser.Text == "root";
            this.baseform = baseform;
        }

        private void accept_Click(object sender, System.EventArgs e)
        {
            IPAddress ip;
            if (!IPAddress.TryParse(serverip.Text, out ip))
            {
                MessageBox.Show("IP-адрес для подключения к серверу неверен!");
                serverip.Text = Config.GetConfig("Network", "Host");
                return;
            }

            try
            {
                uint port = Convert.ToUInt32(serverport.Text);
                if (!Service.isPort(port))
                {
                    MessageBox.Show("Порт для подключения к серверу неверен!");
                    serverport.Text = Config.GetConfig("Network", "Port");
                    return;
                }
            }
            catch
            {
                MessageBox.Show("Порт для подключения к серверу неверен!");
                serverport.Text = Config.GetConfig("Network", "Port");
                return;
            }

            if (!IPAddress.TryParse(sqlip.Text, out ip))
            {
                MessageBox.Show("IP-адрес для подключения к MySQL неверен!");
                sqlip.Text = Config.GetConfig("MySQL", "Host");
                return;
            }

            Config.SetConfig("Network", "Host", serverip.Text);
            Config.SetConfig("Network", "Port", serverport.Text);
            Config.SetConfig("Network", "Closed", serverclosed.Checked ? "1" : "0");
            Config.SetConfig("MySQL", "Host", sqlip.Text);
            Config.SetConfig("MySQL", "Database", sqldb.Text);
            Config.SetConfig("MySQL", "Username", sqluser.Text);
            Config.SetConfig("MySQL", "Password", sqlpass.Text);
            baseform.закрытьСерверToolStripMenuItem.Text = serverclosed.Checked ? "Открыть сервер" : "Закрыть сервер";
            MessageBox.Show("Настройки записаны в файл chater.ini!");

            Close();
        }

        private void sqlstandartdb_CheckedChanged(object sender, System.EventArgs e)
        {
            sqldb.ReadOnly = sqlstandartdb.Checked;
            sqldb.Text = sqlstandartdb.Checked ? "chater" : Config.GetConfig("MySQL", "Database");
        }

        private void sqlroot_CheckedChanged(object sender, System.EventArgs e)
        {
            sqluser.ReadOnly = sqlroot.Checked;
            sqluser.Text = sqlroot.Checked ? "root" : Config.GetConfig("MySQL", "Username");
        }

        private void sqllocalhost_CheckedChanged(object sender, EventArgs e)
        {
            sqlip.ReadOnly = sqllocalhost.Checked;
            sqlip.Text = sqllocalhost.Checked ? "127.0.0.1" : Config.GetConfig("MySQL", "Host");
        }

        private void localhost_CheckedChanged(object sender, System.EventArgs e)
        {
            serverip.ReadOnly = localhost.Checked || all.Checked;
            if (localhost.Checked)
            {
                all.Checked = false;
                serverip.Text = "127.0.0.1";
            }
            else
                serverip.Text = Config.GetConfig("Network", "Host");
        }

        private void all_CheckedChanged(object sender, EventArgs e)
        {
            serverip.ReadOnly = all.Checked || localhost.Checked;
            if (all.Checked)
            {
                localhost.Checked = false;
                serverip.Text = "0.0.0.0";
            }
            else
                serverip.Text = Config.GetConfig("Network", "Host");
        }
    }
}